package cate.game.event.master;

import cate.common.table.d.GDMall;
import cate.game.event.GameEventListener;
import cate.game.role.Role;
import cate.game.role.bag.item.msg.BagCapacityResp;
import cate.game.role.base.VipCsMailHandler;
import cate.game.role.mall.Mall;
import easy.java.practice.errorcode.EcResult;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class VipChangeListener implements GameEventListener<VipChangeEvent> {
    @Override
    public void onEvent(VipChangeEvent gameEvent) {
        Role role = gameEvent.role;
        if (role != null) {
            int oldVip = gameEvent.oldVip;
            int newVip = gameEvent.vip;

            //记录
            role.getHistory().action.vipLevel(gameEvent.vip);
            safe(() -> role.getActivity().checkOpen(), "activity checkOpen");
//            role.getHistory().action.vipGetCreateDay(gameEvent.vip, role.getStatus().getRegisterDay());
            // 英雄背包容量更新
            role.getBag().hero.updateCapacity();
            role.sendNow(new BagCapacityResp(role));
            //藏宝阁增加刷新次数
            EcResult<Mall> r = role.getMall().getMall(GDMall.TREASHURE_SHOP_ID);
            if (r.ok()) {
                r.data.payTimesChange(oldVip, newVip);
            }

            // vip客服邮件
            VipCsMailHandler.exe(role, newVip);
            role.getPay().gift.checkCondition();
        }
    }

    private void safe(Runnable runnable, String description) {
        try {
            runnable.run();
        } catch (Exception e) {
            log.error(description + "报错拉");
        }
    }
}
